package com.xiaochi.subject.application.controller;/**
 * @author zqx
 * @since 2024/7/20
 */

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.xiaochi.subject.application.convert.SubjectCategoryDTOConverter;
import com.xiaochi.subject.application.convert.SubjectLabelDTOConverter;
import com.xiaochi.subject.application.dto.SubjectCategoryDTO;
import com.xiaochi.subject.application.dto.SubjectLabelDTO;
import com.xiaochi.subject.common.entity.Result;
import com.xiaochi.subject.domain.convert.SubjectLabelConverter;
import com.xiaochi.subject.domain.entity.SubjectCategoryBO;
import com.xiaochi.subject.domain.entity.SubjectLabelBO;
import com.xiaochi.subject.domain.service.SubjectCategoryDomainService;
import com.xiaochi.subject.infra.basic.entity.SubjectCategory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 刷题controller
 * @author zqx
 * @since
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增分类
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            //todo 这里加判断是属于日志性能优化   如果不加判断，就算是不打印这个日志，后面的这个对象也会被序列化，从而减低性能
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(),"参数不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.CategoryDTOToCategoryBo(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e); 
            return Result.fail("新增分类失败");
        }
    }

    /**
     * 查询岗位大类
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryPrimaryCategory.subjectCategoryDTO:{}",
                        JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryDTOToCategoryBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryBOListToCategoryDTO(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOS);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 根据分类id查二级分类
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryByPrimary.subjectCategoryDTO:{}",
                        JSON.toJSONString(subjectCategoryDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryDTOToCategoryBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryBOListToCategoryDTO(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOS);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 查询分类及标签
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryAndLabel.subjectCategoryDTO:{}",
                        JSON.toJSONString(subjectCategoryDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryDTOToCategoryBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = new LinkedList<>();
            subjectCategoryBOList.forEach(categoryBO -> {
                SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.CategoryBoToCategoryDTO(categoryBO);
                List<SubjectLabelDTO> labelDTOList = SubjectLabelDTOConverter.INSTANCE.SubjectBOToDTOList(categoryBO.getLabelBOList());
                dto.setLabelDTOList(labelDTOList);
                subjectCategoryDTOList.add(dto);
            });
            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryAndLabel.error:{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.update.subjectCategoryDTO:{}",
                        JSON.toJSONString(subjectCategoryDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryDTOToCategoryBo(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新分类失败");
        }
    }

    /**
     * 删除分类
     */
    @PostMapping("/deleted")
    public Result<Boolean> deleted(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController.deleted.subjectCategoryDTO:{}",
                        JSON.toJSONString(subjectCategoryDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.
                    CategoryDTOToCategoryBo(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.deleted(subjectCategoryBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.deleted.error:{}",e.getMessage(),e);
            return Result.fail("删除分类失败");
        }
    }


}
